package com.example.hcd.tool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

@SuppressLint("ClickableViewAccessibility")  
public class SlideMenu extends HorizontalScrollView {  
  
    private LinearLayout linearLayoutWaper;  
    private ViewGroup viewGroupMenu;  
    private ViewGroup viewGroupContent;  
    private int width;   
  
    private int viewGroupMenuWidth;  
    private int rigthPadding = 100;// dp  
  
    private boolean once = false;  
    private boolean isOpen = false;  
  
    public SlideMenu(Context context, AttributeSet attrs) {  
        super(context, attrs);  
  
        WindowManager wm = (WindowManager) context  
                .getSystemService(Context.WINDOW_SERVICE);  
        DisplayMetrics outMetrics = new DisplayMetrics();  
        wm.getDefaultDisplay().getMetrics(outMetrics);  
        viewGroupMenuWidth = width = outMetrics.widthPixels;  
  
        // dp->px  
        rigthPadding = (int) TypedValue.applyDimension(  
                TypedValue.COMPLEX_UNIT_DIP, 100, context.getResources()  
                        .getDisplayMetrics());  
  
    }  
  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        if (!once) {  
            linearLayoutWaper = (LinearLayout) getChildAt(0);  
            viewGroupMenu = (ViewGroup) linearLayoutWaper.getChildAt(0);  
            viewGroupContent = (ViewGroup) linearLayoutWaper.getChildAt(1);  
            viewGroupMenu.getLayoutParams().width = width - rigthPadding;  
            viewGroupContent.getLayoutParams().width = width;  
            once = true;  
        }  
  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    }  
  
    /** 
     * 偏移量控制 
     */  
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  
        super.onLayout(changed, l, t, r, b);  
        if(changed){  
            this.scrollTo(viewGroupMenuWidth, 0);  
        }  
    }  
  
    /** 
     * 滑动控制 
     */  
    public boolean onTouchEvent(MotionEvent ev) {  
        int action = ev.getAction();  
        switch (action) {  
        case MotionEvent.ACTION_UP:  
            int scollX = getScrollX();  
            if(scollX >= viewGroupMenuWidth/2){  
                this.smoothScrollTo(viewGroupMenuWidth, 0);  
                isOpen = false;  
            }else{  
                this.smoothScrollTo(0, 0);  
                isOpen = true;  
            }  
            return true;  
        }  
        return super.onTouchEvent(ev);  
    }  
  
    /** 
     * 按钮控制 
     */  
    public void toggle(){  
        if(isOpen){  
            if(!isOpen){  
                return;  
            }else{  
                this.smoothScrollTo(0, 0);  
                isOpen = false;  
            }  
        }else{  
            if(isOpen){  
                return;  
            }else{  
                this.smoothScrollTo(viewGroupMenuWidth, 0);  
                isOpen = true;  
            }  
        }  
    }  
  
    /** 
     * 偏移量设置 
     */  
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
        super.onScrollChanged(l, t, oldl, oldt);  
        viewGroupMenu.setTranslationX(l);  
          
  
        /** 
            float scale = l * 1.0f / viewGroupMenuWidth; 
            float leftScale = 1 - 0.3f * scale;   
            float rightScale = 0.865f + scale * 0.2f;  
            viewGroupMenu.setScaleX(leftScale); 
            viewGroupMenu.setScaleY(leftScale); 
            viewGroupMenu.setAlpha(0.6f + 0.4f * (1 - scale)); 
            viewGroupMenu.setTranslationX(viewGroupMenuWidth * scale * 0.5f); 
            viewGroupContent.setPivotX(0); 
            viewGroupContent.setPivotY(viewGroupContent.getHeight()/2); 
            viewGroupContent.setScaleX(rightScale); 
            viewGroupContent.setScaleY(rightScale); 
         */  
          
    }  
      
}  